<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import $api from '@/api/api'

// 借书表单
const borrowForm = reactive({
  cardNo: '',
  barCode: '',
  retDate: ''
})
const borrowLoading = ref(false)

// 还书表单
const returnForm = reactive({
  cardNo: '',
  barCode: ''
})
const returnLoading = ref(false)

// 借书登记
const handleBorrow = async () => {
  borrowLoading.value = true
  try {
    const res = await $api.borrowBook({ ...borrowForm })
    if (res.code === 1) {
      ElMessage.success('借书登记成功')
      borrowForm.cardNo = ''
      borrowForm.barCode = ''
      borrowForm.retDate = ''
    } else {
      ElMessage.error(res.msg || '借书登记失败')
    }
  } catch (e) {
    ElMessage.error('借书登记失败')
  } finally {
    borrowLoading.value = false
  }
}

// 还书登记
const handleReturn = async () => {
  returnLoading.value = true
  try {
    const res = await $api.returnBook({ ...returnForm })
    if (res.code === 1) {
      ElMessage.success('还书登记成功')
      returnForm.cardNo = ''
      returnForm.barCode = ''
    } else {
      ElMessage.error(res.msg || '还书登记失败')
    }
  } catch (e) {
    ElMessage.error('还书登记失败')
  } finally {
    returnLoading.value = false
  }
}
</script>

<template>
  <div class="admin-home-page">
    <div class="form-cards">
      <!-- 借书登记卡片 -->
      <el-card class="form-card borrow-card" shadow="hover">
        <div class="form-title">登记借书</div>
        <el-form :model="borrowForm" class="action-form" label-width="110px">
          <el-form-item label="借书证号">
            <el-input v-model="borrowForm.cardNo" placeholder="请输入借书证号" clearable />
          </el-form-item>
          <el-form-item label="图书条码">
            <el-input v-model="borrowForm.barCode" placeholder="请输入图书条码" clearable />
          </el-form-item>
          <el-form-item label="应还日期">
            <el-date-picker v-model="borrowForm.retDate" type="date" placeholder="请选择应还日期" style="width: 100%" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" :loading="borrowLoading" @click="handleBorrow" class="action-btn">登记借书</el-button>
          </el-form-item>
        </el-form>
      </el-card>

      <!-- 还书登记卡片 -->
      <el-card class="form-card return-card" shadow="hover">
        <div class="form-title">登记还书</div>
        <el-form :model="returnForm" class="action-form" label-width="110px">
          <el-form-item label="借书证号">
            <el-input v-model="returnForm.cardNo" placeholder="请输入借书证号" clearable />
          </el-form-item>
          <el-form-item label="图书条码">
            <el-input v-model="returnForm.barCode" placeholder="请输入图书条码" clearable />
          </el-form-item>
          <el-form-item>
            <el-button type="success" :loading="returnLoading" @click="handleReturn" class="action-btn">登记还书</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<style scoped>
.admin-home-page {
  min-height: 80vh;
  background: linear-gradient(120deg, #e4ecfb 0%, #f5f7fa 100%);
  padding: 60px 0 80px 0;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  position: relative;
  overflow: hidden;
  animation: gradientShift 15s ease infinite;
}

@keyframes gradientShift {
  0% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
  100% { background-position: 0% 50%; }
}

.admin-home-page::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background:
      radial-gradient(circle at 20% 20%, rgba(70,130,180,0.08) 0%, transparent 50%),
      radial-gradient(circle at 80% 80%, rgba(135,206,235,0.08) 0%, transparent 50%),
      linear-gradient(45deg, rgba(70,130,180,0.03) 0%, transparent 70%);
  pointer-events: none;
  animation: backgroundPulse 8s ease-in-out infinite alternate;
}

@keyframes backgroundPulse {
  0% { opacity: 0.5; }
  100% { opacity: 1; }
}

.form-cards {
  display: flex;
  gap: 120px;
  flex-wrap: wrap;
  justify-content: center;
  position: relative;
  z-index: 1;
  perspective: 1000px;
}

.form-card {
  min-width: 600px;
  max-width: 700px;
  height: 560px;
  border-radius: 32px;
  box-shadow: 0 12px 48px 0 rgba(70,130,180,0.12),
  0 2px 4px 0 rgba(70,130,180,0.05);
  background: rgba(255,255,255,0.92);
  backdrop-filter: blur(10px);
  padding: 48px 44px 28px 44px;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  display: flex;
  flex-direction: column;
  justify-content: center;
  position: relative;
  overflow: hidden;
  transform-style: preserve-3d;
}

.form-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 6px;
  background: linear-gradient(90deg, #4682B4 0%, #87CEEB 100%);
  opacity: 0;
  transition: opacity 0.3s;
}

.form-card::after {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(255,255,255,0.8) 0%, transparent 50%);
  opacity: 0;
  transform: scale(0.5);
  transition: all 0.6s ease-out;
  pointer-events: none;
}

.form-card:hover::before {
  opacity: 1;
}

.form-card:hover::after {
  opacity: 0.4;
  transform: scale(1);
}

.form-card:hover {
  box-shadow: 0 20px 60px 0 rgba(70,130,180,0.18),
  0 8px 24px 0 rgba(70,130,180,0.12);
  transform: translateY(-6px) rotateX(2deg) rotateY(-2deg);
  background: rgba(255,255,255,0.96);
}

.form-title {
  font-size: 2.2em;
  font-weight: bold;
  background: linear-gradient(90deg, #4682B4 0%, #87CEEB 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  margin-bottom: 40px;
  letter-spacing: 3px;
  text-align: center;
  text-shadow: 0 4px 16px rgba(70,130,180,0.08);
  position: relative;
  animation: titleFloat 6s ease-in-out infinite;
}

@keyframes titleFloat {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-5px); }
}

.action-form {
  margin-top: 0;
  position: relative;
}

:deep(.el-form-item) {
  margin-bottom: 36px !important;
  position: relative;
  transition: transform 0.3s ease;
}

:deep(.el-form-item:hover) {
  transform: translateX(4px);
}

:deep(.el-form-item__label) {
  font-size: 1.25em;
  color: #4682B4;
  font-weight: 500;
  letter-spacing: 1px;
  transition: color 0.3s;
}

:deep(.el-form-item:hover .el-form-item__label) {
  color: #87CEEB;
}

:deep(.el-input__wrapper),
:deep(.el-date-editor) {
  min-height: 54px;
  font-size: 1em;
  border-radius: 16px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04),
  inset 0 2px 4px rgba(70,130,180,0.06);
  transition: all 0.3s;
  background: rgba(255, 255, 255, 0.8);
}

:deep(.el-input__wrapper:hover),
:deep(.el-date-editor:hover) {
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08),
  inset 0 2px 4px rgba(70,130,180,0.08);
  background: rgba(255, 255, 255, 0.95);
}

:deep(.el-input__inner) {
  font-size: 1.2em;
  height: 54px;
}

.action-btn {
  width: 100%;
  height: 56px;
  font-size: 1.3em;
  border-radius: 28px;
  letter-spacing: 2px;
  margin-top: 10px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  overflow: hidden;
}

.action-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, rgba(255,255,255,0.2), rgba(255,255,255,0));
  transform: skewX(-25deg);
  transition: all 0.75s;
}

.action-btn::after {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(255,255,255,0.3) 0%, transparent 60%);
  opacity: 0;
  transition: opacity 0.4s;
}

.action-btn:hover::before {
  left: 100%;
}

.action-btn:active::after {
  opacity: 1;
}

.borrow-card .action-btn {
  background: linear-gradient(90deg, #4682B4 0%, #87CEEB 100%);
  color: #fff;
  border: none;
  font-weight: bold;
  box-shadow: 0 4px 16px rgba(70,130,180,0.15),
  0 8px 24px -4px rgba(70,130,180,0.25);
}

.borrow-card .action-btn:hover {
  background: linear-gradient(90deg, #5693C5 0%, #98DFF9 100%);
  box-shadow: 0 6px 20px rgba(70,130,180,0.25),
  0 12px 28px -6px rgba(70,130,180,0.35);
  transform: translateY(-2px) scale(1.02);
}

.return-card .action-btn {
  background: linear-gradient(90deg, #67c23a 0%, #95d475 100%);
  color: #fff;
  border: none;
  font-weight: bold;
  box-shadow: 0 4px 16px rgba(103,194,58,0.15),
  0 8px 24px -4px rgba(103,194,58,0.25);
}

.return-card .action-btn:hover {
  background: linear-gradient(90deg, #85ce61 0%, #b3e19d 100%);
  box-shadow: 0 6px 20px rgba(103,194,58,0.25),
  0 12px 28px -6px rgba(103,194,58,0.35);
  transform: translateY(-2px) scale(1.02);
}

@media (max-width: 900px) {
  .form-cards {
    flex-direction: column;
    gap: 48px;
    align-items: center;
    padding: 0 16px;
    perspective: none;
  }

  .form-card {
    min-width: 100%;
    max-width: 100%;
    height: auto;
    padding: 32px 24px 24px 24px;
    transform-style: flat;
  }

  .form-card:hover {
    transform: translateY(-4px);
  }

  .form-title {
    font-size: 1.8em;
  }

  :deep(.el-form-item__label) {
    font-size: 1.1em;
  }

  :deep(.el-form-item:hover) {
    transform: none;
  }
}
</style>
